﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OhMyGol.UI.Models;
using OhMyGol.UI.Models.Grid;

namespace OhMyGol.UI.Controllers
{
    public class PlayersController : Controller
    {
        private IQueryable<PlayerDTO> playersCollection = new List<PlayerDTO>
        {
            new PlayerDTO {Id =  1, Name = "Ojeda, Marcelo",      Position = "ARQ", TeamName = "Tenerife",     Price =  450000 },
            new PlayerDTO {Id =  2, Name = "Ameli, Horacio",      Position = "DEF", TeamName = "R. Vallecano", Price =  950000 },
            new PlayerDTO {Id =  3, Name = "Ayala, Roberto",      Position = "DEF", TeamName = "Napoli",       Price = 1100000 },
            new PlayerDTO {Id =  4, Name = "Chamot, José",        Position = "DEF", TeamName = "Lazio",        Price = 1000000 },
            new PlayerDTO {Id =  5, Name = "Sensini, Roberto",    Position = "DEF", TeamName = "Parma",        Price = 1000000 },
            new PlayerDTO {Id =  6, Name = "López, Gustavo",      Position = "VOL", TeamName = "R. Zaragoza",  Price = 1100000 },
            new PlayerDTO {Id =  7, Name = "Redondo, Fernando",   Position = "VOL", TeamName = "Real Madrid",  Price = 1500000 },
            new PlayerDTO {Id =  8, Name = "Simeone, Diego",      Position = "VOL", TeamName = "At. Madrid",   Price = 2000000 },
            new PlayerDTO {Id =  9, Name = "Veron, Juan S.",      Position = "VOL", TeamName = "Sampdoria",    Price = 1200000 },
            new PlayerDTO {Id = 10, Name = "Balbo, Abel",         Position = "DEL", TeamName = "Roma",         Price = 2000000 },
            new PlayerDTO {Id = 11, Name = "Batistuta, Gabriel",  Position = "DEL", TeamName = "Fiorentina",   Price = 3200000 },
            new PlayerDTO {Id = 12, Name = "Crespo, Hernan",      Position = "DEL", TeamName = "Parma",        Price = 1500000 },
            new PlayerDTO {Id = 13, Name = "Pizzi, Juan Antonio", Position = "DEL", TeamName = "Barcelona",    Price = 1500000 }
        }.AsQueryable();

        public JsonResult Page(GridCriteria gridCriteria)
        {
            var allPlayers = this.playersCollection.Select(p => p);
            var sortedPlayers = gridCriteria.Sort(allPlayers);
            var pagedPlayers = gridCriteria.Page(sortedPlayers);

            int totalRowsCount = allPlayers.Count();
            int pagesCount = gridCriteria.PagesCount(totalRowsCount);
            int pageIndex = gridCriteria.PageIndex;

            var playersPage = new GridPage<PlayerDTO>(
                pagesCount, 
                pageIndex, 
                totalRowsCount, 
                pagedPlayers, 
                ToColumns, 
                GetIndex);

            return Json(playersPage.ToObject());
        }

        private string[] ToColumns(PlayerDTO player)
        {
            return new string[] { player.Position, player.Name, player.TeamName, player.Price.ToString(), player.Id.ToString() };
        }

        private int GetIndex(PlayerDTO player)
        {
            return player.Id;
        }
    }
}
